package gui;

import java.awt.Cursor;
import java.awt.Font;
import java.awt.font.TextAttribute;
import java.util.Hashtable;
import java.util.Map;
import javax.swing.*;

import databas.User_DB;
import Projektarbete.ContactTabListFiller;
import Projektarbete.User;

/**
 * 
 * @author Calle
 */
public class RegisterDialog extends javax.swing.JDialog {

	private JCheckBox checkUnderstood;
	private JLabel agreeText;
	private JTextField eMail;
	private JTextField eMail2;
	private JTextField usernameInput;
	private JLabel surnameLabel;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel usernameLabel;
	private JLabel firstnameLabel;
	private JLabel jLabel7;
	private JLabel jLabel8;
	private JPanel jPanel1;
	private JSeparator jSeparator1;
	private JPasswordField password;
	private JPasswordField password2;
	private JButton registerButton;
	private JLabel registerText;
	private JTextField ssnText;
	private JTextField firstnameInput;
	private JTextField surnameInput;
	private Map<TextAttribute, Object> map;
	private Font mainFont;

	public RegisterDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	@SuppressWarnings("unchecked")
	private void initComponents() {

		jPanel1 = new JPanel();
		jSeparator1 = new JSeparator();
		registerText = new JLabel("Register");
		surnameLabel = new JLabel("Surname:");
		jLabel2 = new JLabel("Password:");
		jLabel3 = new JLabel("Password(again):");
		jLabel4 = new JLabel("E-mail:");
		usernameLabel = new JLabel("Username:");
		firstnameLabel = new JLabel("First name:");
		jLabel7 = new JLabel("E-mail(again):");
		jLabel8 = new JLabel("Social Security Number:");
		agreeText = new JLabel("License & Agreements");
		checkUnderstood = new JCheckBox("I have read and understood the");
		registerButton = new JButton("Register");
		usernameInput = new JTextField();
		firstnameInput = new JTextField();
		surnameInput = new JTextField();
		eMail = new JTextField();
		eMail2 = new JTextField();
		password = new JPasswordField();
		password2 = new JPasswordField();
		ssnText = new JTextField();

		map = new Hashtable<TextAttribute, Object>();
		map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
		mainFont = new Font("Tahoma", Font.PLAIN, 14);

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		registerText.setFont(new java.awt.Font("SansSerif", 0, 18));

		surnameLabel.setFont(mainFont);
		jLabel2.setFont(mainFont);
		jLabel3.setFont(mainFont);
		jLabel4.setFont(mainFont);
		usernameLabel.setFont(mainFont);
		firstnameLabel.setFont(mainFont);
		jLabel7.setFont(mainFont);
		jLabel8.setFont(mainFont);
		checkUnderstood.setFont(mainFont);
		agreeText.setFont(mainFont);

		agreeText.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				clickAgree(evt);
			}

			public void mouseEntered(java.awt.event.MouseEvent evt) {
				underlineText(evt);
			}

			public void mouseExited(java.awt.event.MouseEvent evt) {
				plainText(evt);
			}
		});

		registerButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				registerUser(evt);
			}
		});
		fixLayout();
	}

	private void fixLayout() {
		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				jLabel8)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				ssnText,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				214,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jLabel3)
																						.addComponent(
																								jLabel2)
																						.addComponent(
																								jLabel7)
																						.addComponent(
																								jLabel4)
																						.addComponent(
																								surnameLabel)
																						.addComponent(
																								firstnameLabel)
																						.addComponent(
																								usernameLabel))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								usernameInput,
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								214,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								firstnameInput,
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								214,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								surnameInput,
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								214,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								eMail,
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								214,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								eMail2,
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								214,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								password,
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								214,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								password2,
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								214,
																								javax.swing.GroupLayout.PREFERRED_SIZE)))
														.addComponent(
																jSeparator1,
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				checkUnderstood)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				agreeText,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				148,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																jPanel1Layout
																		.createSequentialGroup()
																		.addGap(0,
																				0,
																				Short.MAX_VALUE)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								jPanel1Layout
																										.createSequentialGroup()
																										.addComponent(
																												registerText)
																										.addGap(292,
																												292,
																												292))
																						.addComponent(
																								registerButton,
																								javax.swing.GroupLayout.Alignment.TRAILING))))
										.addContainerGap()));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(registerText)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jSeparator1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												10,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(usernameLabel)
														.addComponent(
																usernameInput,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(3, 3, 3)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(firstnameLabel)
														.addComponent(
																firstnameInput,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(surnameLabel)
														.addComponent(
																surnameInput,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel4)
														.addComponent(
																eMail,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel7)
														.addComponent(
																eMail2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(6, 6, 6)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																password,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel3)
														.addComponent(
																password2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel8)
														.addComponent(
																ssnText,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												14, Short.MAX_VALUE)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(agreeText)
														.addComponent(
																checkUnderstood))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(registerButton)
										.addGap(16, 16, 16)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

		pack();
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setResizable(false);

		setLocation(getParent().getX(), getParent().getY());
		setVisible(true);
	}

	private void clickAgree(java.awt.event.MouseEvent evt) {
		// show license&agreements
	}

	private void underlineText(java.awt.event.MouseEvent evt) {
		mainFont = mainFont.deriveFont(map);
		agreeText.setFont(mainFont);
		jPanel1.setCursor(new Cursor(Cursor.HAND_CURSOR));
	}

	private void plainText(java.awt.event.MouseEvent evt) {
		agreeText.setFont(new Font("Tahoma", Font.PLAIN, 14));
		jPanel1.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	}

	private void registerUser(java.awt.event.MouseEvent evt) {
		boolean isInteger = true;
		try {
			Integer.parseInt(ssnText.getText());
		} catch (NumberFormatException e) {
			isInteger = false;
		}
		if (!(eMail.getText().isEmpty() || eMail2.getText().isEmpty()
				|| password.getText().isEmpty()
				|| password2.getText().isEmpty()
				|| surnameInput.getText().isEmpty()
				|| usernameInput.getText().isEmpty() || firstnameInput.getText().isEmpty() || ssnText
				.getText().isEmpty())) {

			if (eMail2.getText().equals(eMail.getText())
					&& password.getText().equals(password2.getText())) {

				if (checkUnderstood.isSelected()) {
					if (isInteger) {
						User user = new User(usernameInput.getText(),
								firstnameInput.getText(), surnameInput.getText(),
								eMail.getText(), password.getText(),
								Integer.parseInt(ssnText.getText()));
						dispose();
						
						if (!new ContactTabListFiller().registerNewUser(user)) {

							JOptionPane.showMessageDialog(null,
									"Username or mail already in use!");
						}

					} else {
						JOptionPane.showMessageDialog(null,
								"Social Serial Number needs to be a number!");
					}

				} else {
					JOptionPane.showMessageDialog(null,
							"Please check in 'read License & agreements'");
				}
			} else {
				JOptionPane.showMessageDialog(null,
						"Mail and/or Password does not match");
			}
		} else {
			JOptionPane.showMessageDialog(null,
					"You need to fill in all of the spaces!");
		}

	}
}
